package 一稀疏数组和队列;

import java.util.Scanner;

/**
 * @Author XuZhuHong
 * @CreateTime 2021/9/7 21:42
 * 稀疏数组和队列.环形队列
 */
public class 环形队列 {
    public static void main(String[] args) {
        //测试一把
        System.out.println("测试数组模拟环形队列的案例~~~");
// 创建一个环形队列
        CircleArray queue = new CircleArray(4); //说明设置 4, 其队列的有效数据最大是 3
        char key = ' '; // 接收用户输入
        Scanner scanner = new Scanner(System.in);//
        boolean loop = true;
// 输出一个菜单

        while (loop) {
            System.out.println("s(show): 显示队列");
            System.out.println("e(exit): 退出程序");
            System.out.println("a(add): 添加数据到队列");
            System.out.println("g(get): 从队列取出数据");
            System.out.println("h(head): 查看队列头的数据");
            key = scanner.next().charAt(0);// 接收一个字符
            switch (key) {
                case 's':
                    queue.showQueue();

                    break;
                case 'a':
                    System.out.println("输出一个数");
                    int value = scanner.nextInt();
                    queue.addQueue(value);
                    break;
                case 'g': // 取出数据
                    try {
                        int res = queue.getQueue();
                        System.out.printf("取出的数据是%d\n", res);
                    } catch (Exception e) {
// TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h': // 查看队列头的数据
                    try {
                        int res = queue.headQueue();
                        System.out.printf("队列头的数据是%d\n", res);
                    } catch (Exception e) {
// TODO: handle exception
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e': // 退出
                    scanner.close();

                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出~~");
    }
}

class CircleArray {
    int maxSize;
    int rear;//队列尾部
    int front;//队列头
    int[] array;

    public CircleArray(int size) {
        maxSize = size;
        array = new int[maxSize];
        rear = 0;
        front = 0;
    }

    //显示所有
    public void showQueue() {
        if (isEmpty()) {
            System.out.println("队列空，不能得到数据~");
        }
        for (int i = front; i < front+size(); i++) {
            System.out.printf("arr[%d]=%d\n", i % maxSize, array[i % maxSize]);
        }
    }

    //添加数据
    public void addQueue(int value) {
        if (isFull()) {
            System.out.println("队列满，不能加入数据~");
            return;
        }
        array[rear] = value;
        rear = (rear + 1) % maxSize;
    }

    //得到数据
    public int getQueue() {
        if (isEmpty()) {
            System.out.println("队列空，不能得到数据~");
        }
        int values = array[front];
        front = (front + 1) % maxSize;
        return values;
    }

    //展示数据
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列空，不能得到数据~");
        }
        return array[front];
    }

    // 判断队列是否满
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    // 求出当前队列有效数据的个数
    public int size() {
// rear = 2
// front = 1
// maxSize = 3
        return (rear + maxSize - front) % maxSize;
    }

}
